<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
class Bufa extends Common
{
    public function initialize()
    {
        parent::initialize();
    }

    //补发程序
    public function index()
    {
        $users=db('users')->field('id,recount,relevel,rpath,money_benjin')->where('id','gt',302)->where('recount','gt',1)->where('sign',0)->where('is_lock',0)->select();
        foreach ($users as $value){
            $level=getulevel(1);
            $summey=db('licai_order')->where('uid',$value['id'])->sum('summey');
            $bonus=getbonus($value['id']);
            $vb=$bonus['b1'];
            $rpath=$value['rpath'].$value['id'];
            //推荐两人时候的分享奖
            if($value['recount']>=2&&$summey>0){
                $list_id=db('users')
                    ->field('id,mobile')
                    ->where('up_userid',$value['id'])
                    ->where('activation',1)
                    ->select();
                foreach ($list_id as $key=>$val){
                    $bonus0=getbonus($val['id']);
                    $bl=$level['b5']/100;
                    $mey=$bonus0['b1']*$bl;
                    $mey=$vb<$mey?$vb:$mey;//判断烧伤
                    //判断封顶
                    $mey=$summey-$mey>0?$mey:$summey;
                    if($mey>0){
                        $beizhu2="获得第:1代会员".$val['mobile']."分享算力:".$bonus0['b1']." x ".$bl;
                        jifen(3,$value['id'],$mey,12,1,$beizhu2);
                    }
                    if($summey-$mey<=0){
                        $summey=0;
                        break;
                    }else{
                        $summey=$summey-$mey;
                    }
                }
                //推荐三人时候的分享奖
                if($value['recount']>=3&&$summey>0){
                    $relevel=$value['relevel']+2;
                    $list=db('users')
                        ->field('id,mobile')
                        ->where('relevel',$relevel)
                        ->where('rpath','like','%'.$rpath.'%')
                        ->where('activation',1)
                        ->select();
                    foreach ($list as $key=>$val){
                        $bonus1=getbonus($val['id']);
                        $bl1=$level['b6']/100;
                        $mey1=$bonus1['b1']*$bl1;
                        $mey1=$vb<$mey1?$vb:$mey1;//判断烧伤
                        //判断封顶
                        $mey1=$summey-$mey1>0?$mey1:$summey;
                        if($mey1>0){
                            $beizhu3="获得第:2代会员".$val['mobile']."分享算力:".$bonus1['b1']." x ".$bl1;
                            jifen(3,$value['id'],$mey1,12,1,$beizhu3);
                        }
                        if($summey-$mey1<=0){  //如果封顶直接跳出循环，并且给summey赋值为0，下面的奖金就不会发了
                            $summey=0;
                            break;
                        }else{
                            $summey=$summey-$mey1;
                        }
                    }
                    //推荐五人时候的分享奖
                    if($value['recount']>=5&&$summey>0){
                        $relevel1=$value['relevel']+3;
                        $list1=db('users')
                            ->field('id,mobile')
                            ->where('relevel',$relevel1)
                            ->where('rpath','like','%'.$rpath.'%')
                            ->where('activation',1)
                            ->select();
                        foreach ($list1 as $key=>$val){
                            $bonus2=getbonus($val['id']);
                            $bl2=$level['b7']/100;
                            $mey2=$bonus2['b1']*$bl2;
                            $mey2=$vb<$mey2?$vb:$mey2;//判断烧伤
                            //判断封顶
                            $mey2=$summey-$mey2>0?$mey2:$summey;
                            if($mey2>0){
                                $beizhu4="获得第:3代会员".$val['mobile']."分享算力:".$bonus2['b1']." x ".$bl2;
                                jifen(3,$value['id'],$mey2,12,1,$beizhu4);
                            }
                            if($summey-$mey2<=0){
                                $summey=0;
                                break;
                            }else{
                                $summey=$summey-$mey2;
                            }

                        }

                        //推荐七人时候的分享奖
                        if($value['recount']>=7&&$summey>0){
                            $relevel2=$value['relevel']+4;
                            $list2=db('users')
                                ->field('id,mobile')
                                ->where('relevel',$relevel2)
                                ->where('rpath','like','%'.$rpath.'%')
                                ->where('activation',1)
                                ->select();
                            foreach ($list2 as $key=>$val){
                                $bonus3=getbonus($val['id']);
                                $bl3=$level['b8']/100;
                                $mey3=$bonus3['b1']*$bl3;
                                $mey3=$vb<$mey3?$vb:$mey3;//判断烧伤
                                //判断封顶
                                $mey3=$summey-$mey3>0?$mey3:$summey;
                                if($mey3>0){
                                    $beizhu5="获得第:4代会员".$val['mobile']."分享算力:".$bonus3['b1']." x ".$bl3;
                                    jifen(3,$value['id'],$mey3,12,1,$beizhu5);
                                }
                                if($summey-$mey3<=0){
                                    $summey=0;
                                    break;
                                }else{
                                    $summey=$summey-$mey3;
                                }
                            }
                            //推荐十人时候的分享奖
                            if($value['recount']>=10&&$summey>0){
                                $relevel3=$value['relevel']+5;
                                $relevel4=$value['relevel']+10;
                                $list3=db('users')
                                    ->field('id,mobile')
                                    ->where('plevel','between',[$relevel3,$relevel4])
                                    ->where('rpath','like','%'.$rpath.'%')
                                    ->where('activation',1)
                                    ->select();
                                foreach ($list3 as $key=>$val){
                                    $bonus4=getbonus($val['id']);
                                    $bl4=$level['b9']/100;
                                    $mey4=$bonus4['b1']*$bl4;
                                    $mey4=$vb<$mey4?$vb:$mey4;//判断烧伤
                                    //判断封顶
                                    $mey4=$summey-$mey4>0?$mey4:$summey;
                                    if($mey4>0){
                                        $beizhu3="获得第:5代会员".$val['mobile']."分享算力:".$bonus4['b1']." x ".$bl4;
                                        jifen(3,$value['id'],$mey4,12,1,$beizhu3);
                                    }
                                    if($summey-$mey4<=0){
                                        $summey=0;
                                        break;
                                    }else{
                                        $summey=$summey-$mey4;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            //分享奖层数计算完成

            //修改理财订单加速释放（从最后一条开始加速）
            $licai_orders=db('licai_order')
                ->field('id,summey,mey')
                ->where(array('uid'=>$value['id']))
                ->where('summey','gt',0)
                ->order('createtime asc')
                ->select();
            $bonus11=getbonus($value['id']);//获取会员当天奖金统计表（bonus）
            $bmey=$bonus11['b2'];//当天 加速了多少（这个方法只能在这里）
            if($bmey>0){
                foreach ($licai_orders as $key=>$v){
                    if($v['summey']-$bmey>0){
                        $data['summey']=$v['summey']-$bmey;
                        $bmey=0;
                    }else{
                        $bmey=$bmey-$v['summey'];
                        $beizhuchuju="会员动态奖出局转出金额".$v['mey'];
                        mey_log(3,$value['id'],2,18,$v['mey'],$v['mey'],$beizhuchuju);
                        $data['summey']=0;
                        $data['start']=2;
                    }
                    db('licai_order')->where('id',$v['id'])->update($data);
                    if($bmey<=0){
                        break;
                    }
                }
                $beizhu4="链接算力钱包释放到流通钱包,释放金额:".$bonus11['b2'];
                mey_log(3,$value['id'],2,13,$bonus11['b2'],$value['money_benjin']+$bonus11['b2'],$beizhu4);
            }
        }
    }
}